package com.xxl.job.admin.controller.handle;

import com.xxl.job.admin.dto.exception.LogicBusinessException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 注意使用注解@ControllerAdvice作用域是全局Controller范围
 * * 可应用到所有@RequestMapping类或方法上的@ExceptionHandler、@InitBinder、@ModelAttribute，在这里是@ExceptionHandler<br />
 * * 用于检测第三方接口,比如bsvc或cia的504,502等异常<br />
 * * 这些异常均属于非业务异常,与业务毫无关系,所以单独处理<br />
 * * 注意:StoreBusinessException 不要捕获,否则无法被BusinessExceptionHandlerAdvice 截获<br />
 * * 注意:传递url中的参数如果可能包含中文一定要URL编码,
 */
@ControllerAdvice
public class BusinessExceptionHandlerAdvice {

    @ExceptionHandler({NoHandlerFoundException.class})
    public String handle404Exception4(BindException ex, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        return handle400Action(ex, "404,not found", request, response);
    }

    private String handle400Action(Exception ex, Object value, HttpServletRequest request, HttpServletResponse response) {
        String respCode = String.valueOf(HttpServletResponse.SC_BAD_REQUEST);
//        logger.error(respCode, ex);
        LogicBusinessException logicBusinessException = new LogicBusinessException();
        logicBusinessException.setErrorCode(respCode);
        logicBusinessException.setErrorMessage((value == null ? "" : value + " ") + ex.getMessage());
//        BusinessExceptionUtil.dealException(logicBusinessException, response);
        return null;
    }

}
